
package com.joysuch.wwyt.core.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.transaction.Transactional;

import com.joysuch.wwyt.core.bean.SimpleSelectListBean;
import com.joysuch.wwyt.core.service.SimpleSelectListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

@Service
@Transactional
public class SimpleSelectListServiceImpl implements SimpleSelectListService {

    @Autowired
    private EntityManager em;

    @Override
    public Page<SimpleSelectListBean> page(Pageable pageList, Class<?> entityClass, String textFieldName)
            throws UnsupportedOperationException {
        return page(pageList, entityClass, "id", textFieldName);
    }

    @Override
    public Page<SimpleSelectListBean> page(Pageable pageList, Class<?> entityClass, String idFieldName,
            String textFieldName) throws UnsupportedOperationException {
        return page(pageList, entityClass, idFieldName, textFieldName, Integer.MAX_VALUE);
    }

    @Override
    public Page<SimpleSelectListBean> page(Pageable pageList, Class<?> entityClass, String idFieldName,
            String textFieldName, int maxResults) throws UnsupportedOperationException {
        String hql = "select " + idFieldName + " as id," + textFieldName + " as name from "
                + entityClass.getName()/* + " limit " + pageList.getPage() +","+ pageList.getSize() */;
        Query query = em.createQuery(hql);
        query.setFirstResult((pageList.getPageNumber()) * pageList.getPageSize());
        query.setMaxResults(pageList.getPageSize());
        String countHql = "select count(m) from " + entityClass.getName() + " m";
        Long count = em.createQuery(countHql, Long.class).getSingleResult();
        // query.setMaxResults(maxResults);
        query.getResultList();
        List<Object[]> resultList = query.getResultList();
        List<SimpleSelectListBean> list = new ArrayList<>(resultList.size());
        for (Object[] o : resultList) {
            SimpleSelectListBean b = new SimpleSelectListBean();
            b.setValue((Long) o[0]);
            b.setLabel((String) o[1]);
            list.add(b);
        }
        return new PageImpl(list, pageList, count);
    }

    @Override
    public List<SimpleSelectListBean> list(Class<?> entityClass, String textFieldName)
            throws UnsupportedOperationException {
        return list(entityClass, "id", textFieldName);
    }

    @Override
    public List<SimpleSelectListBean> list(Class<?> entityClass, String idFieldName, String textFieldName)
            throws UnsupportedOperationException {
        return list(entityClass, idFieldName, textFieldName, Integer.MAX_VALUE);
    }

    @Override
    public List<SimpleSelectListBean> list(Class<?> entityClass, String idFieldName, String textFieldName,
            int maxResults) throws UnsupportedOperationException {
        String hql = "select " + idFieldName + " as id," + textFieldName + " as name from " + entityClass.getName();
        Query query = em.createQuery(hql);
        query.setMaxResults(maxResults);
        query.getResultList();
        List<Object[]> resultList = query.getResultList();
        List<SimpleSelectListBean> list = new ArrayList<>(resultList.size());
        for (Object[] o : resultList) {
            SimpleSelectListBean b = new SimpleSelectListBean();
            b.setValue((Long) o[0]);
            b.setLabel((String) o[1]);
            list.add(b);
        }
        return list;
    }

}
